﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Innova.Microfinanzas.BusinessEntities;

namespace MyCommerce.Business.GeneralParameters
{
    public class CategoryRelationManager
    {
        private MyCommerceEntities vContext;

        public CategoryRelationManager()
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = false;
            }
        }

        public CategoryRelationManager(bool lazyLoad)
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = lazyLoad;
            }
        }

        public List<CategoryRelation> GetListCategoryRelation()
        {
            List<CategoryRelation> colClassifier = new List<CategoryRelation>();
            var classifierResult = from cl in vContext.CategoryRelation
                                   select cl;

            if (classifierResult != null)
	        {
                colClassifier = new List<CategoryRelation>(classifierResult.AsEnumerable<CategoryRelation>());
	        }

            return colClassifier;
        }

        public List<CategoryRelation> GetListCategoryRelationByIdCategory(long idCategory)
        {
            List<CategoryRelation> colClassifier = new List<CategoryRelation>();
            var classifierResult = from cl in vContext.CategoryRelation
                                   where cl.IdcCategory == idCategory
                                   select cl;

            if (classifierResult != null)
            {
                colClassifier = new List<CategoryRelation>(classifierResult.AsEnumerable<CategoryRelation>());
            }

            return colClassifier;
        }

        public void SaveCategoryRelation(List<CategoryRelation> colCategoryRelation)
        {
            long idCategory = colCategoryRelation[0].IdcCategory;

            var validateResult = from cl in vContext.CategoryRelation
                                   where cl.IdcCategory == idCategory
                                   select cl;
            if(validateResult != null)
                if (validateResult.Count() > 0)
                {
                    foreach (CategoryRelation item in new List<CategoryRelation>(validateResult.AsEnumerable<CategoryRelation>()))
                    {
                        vContext.DeleteObject(item);
                    }
                }

            foreach (CategoryRelation item in colCategoryRelation)
            {
                vContext.CategoryRelation.AddObject(item);
            }
            vContext.SaveChanges();
        }

        public void DeleteClassifier(long idClassifier)
        {
            var vResult = (from cl in vContext.Classifier
                           where cl.IdClassifier == idClassifier
                           select cl).First();
            vContext.DeleteObject(vResult);
            vContext.SaveChanges();
        }
    }

    public class CategoryRelationDetailManager
    { 
        private MyCommerceEntities vContext;

        public CategoryRelationDetailManager()
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = false;
            }
        }

        public CategoryRelationDetailManager(bool lazyLoad)
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = lazyLoad;
            }
        }

        public List<CategoryRelationDetail> GetList()
        { 
            List<CategoryRelation> colClassifier = new List<CategoryRelation>();
            
            List<CategoryRelationDetail> colCategoryRelationDetail = new List<CategoryRelationDetail>();

            var classifierResult = from cl in vContext.CategoryRelation
                                   .Include("Classifier")
                                   .Include("Classifier1")
                                   select cl;

            if (classifierResult != null)
	        {
                colClassifier = new List<CategoryRelation>(classifierResult.AsEnumerable<CategoryRelation>());
	        }

            bool existCategory = false;

            foreach (CategoryRelation item in colClassifier)
            {
                foreach (CategoryRelationDetail objDetail in colCategoryRelationDetail)
                {
                    if (objDetail.ObjCategory.IdClassifier == item.IdcCategory)
                    {
                        existCategory = true;
                    }
                }

                if (!existCategory)
                {
                    List<Classifier> colProductType = new List<Classifier>();
                    foreach (CategoryRelation itemCompate in colClassifier)
                    {
                        if (item.Classifier.IdClassifier == itemCompate.Classifier.IdClassifier)
                        {
                            colProductType.Add(itemCompate.Classifier1);
                        }
                    }
                    CategoryRelationDetail objCategoryRelationDetail = new CategoryRelationDetail();
                    item.Classifier.CategoryRelation = null;
                    objCategoryRelationDetail.ObjCategory = item.Classifier;
                    objCategoryRelationDetail.ColProductType = colProductType;
                    colCategoryRelationDetail.Add(objCategoryRelationDetail);                    
                }
                existCategory = false;
            }
            return colCategoryRelationDetail;
        }

    }
}
